<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="栏目" prop="columnId">
        <el-input
            v-model="queryParams.columnId"
            placeholder="请输入栏目"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="标题" prop="articleTitle">
        <el-input
            v-model="queryParams.articleTitle"
            placeholder="请输入标题"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="回帖状态" prop="articleCommenTable">
        <el-select v-model="queryParams.articleCommenTable" placeholder="请选择" style="width: 150px;" clearable>
          <el-option
              v-for="dict in sys_normal_disable"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="articleStatus">
        <el-select v-model="queryParams.articleStatus" placeholder="请选择" style="width: 150px;" clearable>
          <el-option
              v-for="dict in pc_article_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="类型" prop="articleType">
        <el-select v-model="queryParams.articleType" placeholder="请选择类型" style="width: 150px;" clearable>
          <el-option
              v-for="dict in pc_article_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="用户状态" prop="articleAnonymous">
        <el-select v-model="queryParams.articleAnonymous" placeholder="请选择用户状态" style="width: 150px;" clearable>
          <el-option
              v-for="dict in pc_article_anonymous"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="优选状态" prop="articlePerfect">
        <el-select v-model="queryParams.articlePerfect" placeholder="请选择优选状态" style="width: 150px;" clearable>
          <el-option
              v-for="dict in pc_article_perfect"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="访问状态" prop="articleAnonymousView">
        <el-select v-model="queryParams.articleAnonymousView" placeholder="请选择访问状态" style="width: 150px;" clearable>
          <el-option
              v-for="dict in pc_article_anonymous_view"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="列表展示" prop="articleShowInList">
        <el-select v-model="queryParams.articleShowInList" placeholder="请选择" style="width: 150px;" clearable>
          <el-option
              v-for="dict in pc_article_show_in_list"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
            v-hasPermi="['forum:article:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="success"
            plain
            icon="Edit"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['forum:article:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            icon="Delete"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['forum:article:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['forum:article:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="articleList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />

      <el-table-column label="栏目" align="center" prop="columnId" />
      <el-table-column label="标题" align="center" prop="articleTitle" />
      <el-table-column label="作者" align="center" prop="articleAuthorid" />
      <el-table-column label="类型" align="center" prop="articleType">
        <template #default="scope">
          <dict-tag :options="pc_article_type" :value="scope.row.articleType"/>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="articleStatus">
        <template #default="scope">
          <dict-tag :options="pc_article_status" :value="scope.row.articleStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="回帖状态" align="center" prop="articleCommenTable">
        <template #default="scope">
          <dict-tag :options="sys_normal_disable" :value="scope.row.articleCommenTable"/>
        </template>
      </el-table-column>
      <el-table-column label="回帖数" align="center" prop="articleCommentCount" />
      <el-table-column label="浏览数" align="center" prop="articleViewCount" />
      <el-table-column label="打赏积分" align="center" prop="articleRewardPoint" />
      <el-table-column label="访问路径" align="center" prop="articlePermaLink" />
      <el-table-column label="创建时间" align="center" prop="articleCreateTime" />
      <el-table-column label="更新时间" align="center" prop="articleUpdateTime" />
      <el-table-column label="感谢计数" align="center" prop="articleThankCnt" />
      <el-table-column label="点赞计数" align="center" prop="articleGoodCnt" />
      <el-table-column label="点踩计数" align="center" prop="articleBadCnt" />
      <el-table-column label="收藏计数" align="center" prop="articleCollectCnt" />
      <el-table-column label="关注计数" align="center" prop="articleWatchCnt" />
      <el-table-column label="User-Agent" align="center" prop="articleUa" />
      <el-table-column label="置顶时间" align="center" prop="articleStick" />
      <el-table-column label="作者状态" align="center" prop="articleAnonymous">
        <template #default="scope">
          <dict-tag :options="pc_article_anonymous" :value="scope.row.articleAnonymous"/>
        </template>
      </el-table-column>
      <el-table-column label="优选状态" align="center" prop="articlePerfect">
        <template #default="scope">
          <dict-tag :options="pc_article_perfect" :value="scope.row.articlePerfect"/>
        </template>
      </el-table-column>
      <el-table-column label="浏览状态" align="center" prop="articleAnonymousView">
        <template #default="scope">
          <dict-tag :options="pc_article_anonymous_view" :value="scope.row.articleAnonymousView"/>
        </template>
      </el-table-column>

      <el-table-column label="帖子地址" align="center" prop="articleImgUrl" />
      <el-table-column label="列表状态" align="center" prop="articleShowInList">
        <template #default="scope">
          <dict-tag :options="pc_article_show_in_list" :value="scope.row.articleShowInList"/>
        </template>
      </el-table-column>
      <el-table-column label="AI总结" align="center" prop="aiSummary" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['forum:article:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['forum:article:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加或修改pc_贴子/文章对话框 -->
    <el-dialog :title="title" v-model="open" width="800px" style="height: 650px;" append-to-body>
      <div class="auto-scroll-content">
        <el-form ref="articleRef" :model="form" :rules="rules" label-width="80px">
          <el-row>
            <el-col :span="12">
              <el-form-item label="栏目id" prop="columnId">
                <el-input v-model="form.columnId" placeholder="请输入栏目id" />
              </el-form-item>
              <el-form-item label="栏目ids" prop="columnIds" v-if="false">
                <el-input v-model="form.columnIds" type="textarea" placeholder="请输入内容" />
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="标题" prop="articleTitle">
                <el-input v-model="form.articleTitle" placeholder="请输入标题" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="帖子标签" prop="articleTags">
                <el-input v-model="form.articleTags" placeholder="请输入，英文状态逗号分隔" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="帖子作者" prop="articleAuthorid">
                <el-input v-model="form.articleAuthorid" placeholder="请输入作者" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="回帖数" prop="articleCommentCount">
                <el-input v-model="form.articleCommentCount" placeholder="请输入回帖数" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="浏览数" prop="articleViewCount">
                <el-input v-model="form.articleViewCount" placeholder="请输入浏览数" />
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="正文内容">
                <editor v-model="form.articleContent" :min-height="192"/>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="打赏区内容">
                <editor v-model="form.articleRewardContent" :min-height="192"/>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="打赏积分" prop="articleRewardPoint">
                <el-input v-model="form.articleRewardPoint" placeholder="请输入" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="访问路径" prop="articlePermaLink">
                <el-input v-model="form.articlePermaLink" placeholder="请输入" />
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="是否回帖" prop="articleCommenTable">
                <el-select v-model="form.articleCommenTable" placeholder="请选择">
                  <el-option
                      v-for="dict in sys_normal_disable"
                      :key="dict.value"
                      :label="dict.label"
                      :value="dict.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="状态" prop="articleStatus">
                <el-select v-model="form.articleStatus" placeholder="请选择">
                  <el-option
                      v-for="dict in pc_article_status"
                      :key="dict.value"
                      :label="dict.label"
                      :value="parseInt(dict.value)"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="类型" prop="articleType">
                <el-select v-model="form.articleType" placeholder="请选择">
                  <el-option
                      v-for="dict in pc_article_type"
                      :key="dict.value"
                      :label="dict.label"
                      :value="parseInt(dict.value)"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="发帖城市" prop="articleCity">
                <el-input v-model="form.articleCity" placeholder="请输入" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="发帖IP" prop="articleIp">
                <el-input v-model="form.articleIp" placeholder="请输入" />
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="置顶时间" prop="articleStick">
                <el-input v-model="form.articleStick" placeholder="请输入置顶时间" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="作者状态" prop="articleAnonymous">
                <el-select v-model="form.articleAnonymous" placeholder="请选择">
                  <el-option
                      v-for="dict in pc_article_anonymous"
                      :key="dict.value"
                      :label="dict.label"
                      :value="parseInt(dict.value)"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="优选状态" prop="articlePerfect">
                <el-select v-model="form.articlePerfect" placeholder="请选择">
                  <el-option
                      v-for="dict in pc_article_perfect"
                      :key="dict.value"
                      :label="dict.label"
                      :value="parseInt(dict.value)"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="浏览状态" prop="articleAnonymousView">
                <el-select v-model="form.articleAnonymousView" placeholder="请选择">
                  <el-option
                      v-for="dict in pc_article_anonymous_view"
                      :key="dict.value"
                      :label="dict.label"
                      :value="parseInt(dict.value)"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="悬赏积分" prop="articleQnaOfferPoint">
                <el-input v-model="form.articleQnaOfferPoint" placeholder="请输入" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="周/月邮件" prop="articlePushOrder">
                <el-input v-model="form.articlePushOrder" placeholder="请输入推送优先级" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="列表展示" prop="articleShowInList">
                <el-select v-model="form.articleShowInList" placeholder="请选择">
                  <el-option
                      v-for="dict in pc_article_show_in_list"
                      :key="dict.value"
                      :label="dict.label"
                      :value="parseInt(dict.value)"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>

          </el-row>
        </el-form>
      </div>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Article">
import { listArticle, getArticle, delArticle, addArticle, updateArticle } from "@/api/forum/article";

const { proxy } = getCurrentInstance();
const { pc_article_anonymous, pc_article_type, pc_article_perfect, pc_article_status, pc_article_editor_type, pc_article_show_in_list, sys_normal_disable, pc_article_anonymous_view } = proxy.useDict('pc_article_anonymous', 'pc_article_type', 'pc_article_perfect', 'pc_article_status', 'pc_article_editor_type', 'pc_article_show_in_list', 'sys_normal_disable', 'pc_article_anonymous_view');

const articleList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    columnId: null,
    articleTitle: null,
    articleContent: null,
    articleRewardContent: null,
    articleCommenTable: null,
    articleEditorType: null,
    articleStatus: null,
    articleType: null,
    articleAnonymous: null,
    articlePerfect: null,
    articleAnonymousView: null,
    articleShowInList: null,
  },
  rules: {
    columnId: [
      { required: true, message: "栏目id不能为空", trigger: "blur" }
    ],
    articleTitle: [
      { required: true, message: "帖子标题不能为空", trigger: "blur" }
    ],
    articleCommenTable: [
      { required: true, message: "帖子是否可回帖不能为空", trigger: "change" }
    ],
    articleEditorType: [
      { required: true, message: "0：Markdown不能为空", trigger: "change" }
    ],
    articleStatus: [
      { required: true, message: "0：正常，1：封禁，2：锁定不能为空", trigger: "change" }
    ],
    articleType: [
      { required: true, message: "0：普通帖子，1：机要，2：同城广播，3：思绪不能为空", trigger: "change" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询pc_贴子/文章列表 */
function getList() {
  loading.value = true;
  listArticle(queryParams.value).then(response => {
    articleList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    columnId: null,
    columnIds: null,
    articleTitle: null,
    articleTags: null,
    articleAuthorid: null,
    articleCommentCount: null,
    articleViewCount: null,
    articleContent: null,
    articleRewardContent: null,
    articleRewardPoint: null,
    articlePermaLink: null,
    articleCreateTime: null,
    articleUpdateTime: null,
    articleLatestCmtTime: null,
    articleLatestCmterName: null,
    articleRandomDouble: null,
    articleCommenTable: null,
    articleEditorType: null,
    articleStatus: null,
    articleType: null,
    articleThankCnt: null,
    articleGoodCnt: null,
    articleBadCnt: null,
    articleCollectCnt: null,
    articleWatchCnt: null,
    redditScore: null,
    articleCity: null,
    articleIp: null,
    articleUa: null,
    articleStick: null,
    articleAnonymous: null,
    articlePerfect: null,
    articleAnonymousView: null,
    articleAudioUrl: null,
    articleQnaOfferPoint: null,
    articlePushOrder: null,
    articleImgUrl: null,
    articleShowInList: null,
    aiSummary: null,
    delFlag: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    remark: null
  };
  proxy.resetForm("articleRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加pc_贴子/文章";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getArticle(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改pc_贴子/文章";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["articleRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateArticle(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addArticle(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除pc_贴子/文章编号为"' + _ids + '"的数据项？').then(function() {
    return delArticle(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('forum/article/export', {
    ...queryParams.value
  }, `article_${new Date().getTime()}.xlsx`)
}

getList();
</script>
<style  lang='scss' scoped>
.auto-scroll-content {
  max-height: calc(100vh - 200px); /* 根据实际情况调整 */
  overflow-y: auto;
}
</style>
